package com.wust.smartcity.generatorcode.utils;

import com.wust.smartcity.generatorcode.entity.ColumnInfo;
import com.wust.smartcity.generatorcode.task.*;
import com.wust.smartcity.generatorcode.task.base.AbstractTask;
import com.wust.smartcity.generatorcode.task.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ：wust
 * Date   2018-11-27
 */
public class TaskQueue {

    private LinkedList<AbstractTask> taskQueue = new LinkedList<>();

    private void initCommonTasks(String childEntityPackageName,String className, List<ColumnInfo> tableInfos) {
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getController())) {
            taskQueue.add(new ControllerTask(childEntityPackageName,className));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getService())) {
            taskQueue.add(new ServiceTask(childEntityPackageName,className));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getInterf())) {
            taskQueue.add(new InterfaceTask(childEntityPackageName,className));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getDao())) {
            taskQueue.add(new DaoTask(childEntityPackageName,className));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getVuePath())) {
            taskQueue.add(new VueListTask(className,tableInfos));
            taskQueue.add(new VueCreateTask(className,tableInfos));
            taskQueue.add(new VueUpdateTask(className,tableInfos));
            taskQueue.add(new VueSearchBarTask(className,tableInfos));
        }
    }

    public void initSingleTasks(String childEntityPackageName,String className, String tableName, List<ColumnInfo> tableInfos) {
        initCommonTasks(childEntityPackageName,className,tableInfos);
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getEntityPackageName())) {
            taskQueue.add(new EntityTask(childEntityPackageName,className, tableInfos));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getMapper())) {
            taskQueue.add(new MapperTask(childEntityPackageName,className, tableName, tableInfos));
        }
    }

    public void initOne2ManyTasks(String childEntityPackageName,String tableName, String className, String parentTableName, String parentClassName, String foreignKey, List<ColumnInfo> tableInfos, List<ColumnInfo> parentTableInfos) {
        initCommonTasks(childEntityPackageName,className,tableInfos);
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getEntityPackageName())) {
            taskQueue.add(new EntityTask(childEntityPackageName,className, parentClassName, foreignKey, tableInfos));
            taskQueue.add(new EntityTask(childEntityPackageName,parentClassName, parentTableInfos));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getMapper())) {
            taskQueue.add(new MapperTask(childEntityPackageName,tableName, className, parentTableName, parentClassName, foreignKey, tableInfos, parentTableInfos));
        }
    }

    public void initMany2ManyTasks(String childEntityPackageName,String tableName, String className, String parentTableName, String parentClassName, String foreignKey, String parentForeignKey, String relationalTableName, List<ColumnInfo> tableInfos, List<ColumnInfo> parentTableInfos) {
        initCommonTasks(childEntityPackageName,className,tableInfos);
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getEntityPackageName())) {
            taskQueue.add(new EntityTask(childEntityPackageName,className, parentClassName, foreignKey, parentForeignKey, tableInfos));
            taskQueue.add(new EntityTask(childEntityPackageName,parentClassName, parentTableInfos));
        }
        if (!StringUtil.isBlank(ConfigUtil.getConfiguration().getPath().getMapper())) {
            taskQueue.add(new MapperTask(childEntityPackageName,tableName, className, parentTableName, parentClassName, foreignKey, parentForeignKey, relationalTableName, tableInfos, parentTableInfos));
        }
    }

    public boolean isEmpty() {
        return taskQueue.isEmpty();
    }

    public AbstractTask poll() {
        return taskQueue.poll();
    }

}
